package com.hosp.service;

import java.util.List;
import java.util.Objects;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hosp.bean.Drug;
import com.hosp.controller.dto.DrugDTO;
import com.hosp.mapper.DoctorMapper;
import com.hosp.mapper.DrugMapper;
import com.hosp.mapper.HosregisterMapper;
import com.hosp.vo.PageRequestVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ztranscript 2021/10/17 17:12
 **/
@Slf4j
@Service
public class DrugService {
    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private HosregisterMapper hosregisterMapper;

    @Autowired
    private DrugMapper drugMapper;

    /**
     * 查询列表
     *
     * @param drug        查询条件
     * @param pageRequest 分页条件
     * @return 响应
     */
    public PageInfo<Drug> list(Drug drug, PageRequestVO pageRequest) {
        // 分页查询
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<Drug> drugs = drugMapper.selectByExample(null);

        return new PageInfo<>(drugs);
    }

    /**
     * 保存
     *
     * @param drugDTO 保存对象
     * @return 响应
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean save(DrugDTO drugDTO) {
        if (Objects.isNull(drugDTO.getDrId())) {
            drugMapper.insertSelective(drugDTO);
        } else {
            if (drugDTO.getNumber() != null) {
                drugDTO.setDrNumber(drugDTO.getDrNumber() + drugDTO.getNumber());
            }
            drugMapper.updateByPrimaryKeySelective(drugDTO);
        }
        return true;
    }

    /**
     * 主键查询
     *
     * @param drId 查询条件
     * @return 响应
     */
    public Drug selectById(Integer drId) {
        return drugMapper.selectByPrimaryKey(drId);
    }

    /**
     * 删除
     *
     * @param uIds 待删除 IDs
     * @return 响应
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(List<Integer> uIds) {
        return true;
    }

}
